package com.woniuxy.his.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.his.common.Result;
import com.woniuxy.his.vo.UpdateSymANDStuVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.his.controller.form.AddGuahaoForm;
import com.woniuxy.his.entity.Department;
import com.woniuxy.his.entity.Doctor;
import com.woniuxy.his.service.IGuahaoService;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

import java.util.List;
import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 * 挂号表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@RestController
@Slf4j
@CrossOrigin("*")

public class GuahaoController {

    @Resource
    private IGuahaoService guahaoService;

    @Resource
    private RedissonClient redissonClient;

    //获取挂号信息，在挂号处理页面展示
    @GetMapping("/queryInformationOfGuaHao")
    public Result queryInformationOfGuaHao(
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(name = "current" , defaultValue = "1") int current,
            @RequestParam(name = "doctorId" , defaultValue = "52") int doctorId){
        System.out.println("来了");
        log.info("页容量：pageSize={},当前页：current={},医生id={}",pageSize,current,doctorId);
        IPage iPage = guahaoService.queryInformationOfGuaHao(current, pageSize,doctorId);
        return iPage==null?Result.fail("0000","没找到",iPage):Result.success("1111","找到了",iPage);
    }

    //根据返回的挂号ID,将修改后的病人状态status和症状symptom添加数据库
    @PostMapping("/modifyStatusAndSymptom")
    public Result modifyStatusAndSymptom(@RequestBody UpdateSymANDStuVo updateSymANDStuVo){
        log.info("挂号ID={},症状={},状态={},诊断结果={}",updateSymANDStuVo.getGuaId(),updateSymANDStuVo.getSymptom(),updateSymANDStuVo.getStatus(),updateSymANDStuVo.getDiaResult(),updateSymANDStuVo.getDiaResult());
        Integer integer = guahaoService.modifyStatusAndSymptom(updateSymANDStuVo.getGuaId(), updateSymANDStuVo.getStatus(), updateSymANDStuVo.getSymptom(),updateSymANDStuVo.getDiaResult());

        return integer>0?Result.success("1111","操作成功",1):Result.fail("0000","操作失败",0);
    }



    @GetMapping("/findAllGuahao")
    public Result findAllGuahao(String patientName,Integer size,Integer current){
        log.info("patientName为{},size{},current{}",patientName,size,current);

        IPage pageResult = guahaoService.findGuahaoByPage(patientName, size, current);
        if (null==pageResult) {
            return Result.fail("400","未找到挂号信息","");
        }else{
            return Result.success("200","找到挂号信息",pageResult);
        }
    }

    @PostMapping("/addGuahao")
    public Result addGuahao(@RequestBody AddGuahaoForm addGuahaoForm){
        log.info("添加addForm为{}",addGuahaoForm);
        RBucket<Object> bucket = redissonClient.getBucket("sort");
        bucket.set(6);
        System.out.println("==========================");
        Integer integer = guahaoService.addGuahao(addGuahaoForm);
        if(integer>=1){
            log.info("如果添加成功");
            return Result.success("200","添加成功",integer);
        }
        return Result.fail("400","添加失败",integer);

    }

    @GetMapping("/findDoctors")
    public Result findDoctorsByDepId(Integer depId){
        log.info("得到科室id为：{}",depId);
        List<Doctor> doctors = guahaoService.findDoctorsByDepId(depId);
        if(null!=doctors){
            log.info("医生们为{}",doctors);
            return Result.success("200","找到医生们",doctors);
        }
        return Result.success("200","该科室没有医生",doctors);
    }

    @GetMapping("/findAllDepartment")
    public Result findAllDep(){
        log.info("查询所有科室");
        List<Department> departmentList = guahaoService.findAllDepartment();
        if(null!=departmentList){
            return Result.success("200","查询所有科室成功",departmentList);
        }
        return Result.fail("400","查询所有科室失败",null);

    }

    @GetMapping("/getAges")
    public Result getAges(){
        ArrayList<Integer> integers = new ArrayList<>();
        for(int i=1;i<=100;i++){
            integers.add(i);
        }
        return Result.success("200","循环添加年龄",integers);
    }






}

